package indi.cloud.common.elasticsearch.impl;

/**
 * @author yangyifan
 * @version 1.0
 * @date 2024/1/29 15:04
 * @Description
 */

import indi.cloud.common.elasticsearch.EsIndexService;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 对ElasticSearch 索引的服务类
 *
 * @date: 2020/8/10 00:01
 */
@Service
public class EsIndexServiceImpl implements EsIndexService {
    @Resource
    protected ElasticsearchOperations operations;

    @Override
    public <T> boolean createIndex(Class<T> t) {
        IndexOperations indexOperations = operations.indexOps(t);
        boolean createIndexRes = indexOperations.create();
        if (!createIndexRes) {
            return false;
        }
        Document document = indexOperations.createMapping();
        return indexOperations.putMapping(document);
    }

    @Override
    public <T> boolean deleteIndex(Class<T> t) {
        return operations.indexOps(t).delete();
    }

    @Override
    public <T> boolean reCreate(Class<T> t) {
        IndexOperations indexOperations = operations.indexOps(t);
        return indexOperations.delete() && indexOperations.create();
    }

    @Override
    public <T> boolean indexExists(Class<T> t) {
        return operations.indexOps(t).exists();
    }
}
